﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.Data;
using StoragePeriod.Common;

namespace StoragePeriod.Web.SMS
{
    public partial class MySMS : SecurityPage
    {
        #region 属性

        #endregion

        #region 继承
        public override string PrivCode()
        {
            return "2c7c588c6f951190";
        }
        #endregion

        #region 函数
        private void BingRep()
        {
            SYS_Users md = SYS_Users.Where("UserCode=@0", User.Identity.Name).First();
            int sPid = Convert.ToInt32(md.id);
            using (StoragePeriodDataContext data = new StoragePeriodDataContext())
            {
                int rowcount = data.SendList.Where(p => p.PersonnelID == sPid && p.ReceiverDel == false && p.SendType == Convert.ToInt32(EnumData.SendType.短讯)).Count();
                int pagerowcount = Convert.ToInt32(this.lab_count.Text);
                int currpageindex = Convert.ToInt32(this.lab_CurrPage.Text);
                int pagecount = (rowcount % pagerowcount > 0) ? rowcount / pagerowcount + 1 : rowcount / pagerowcount;
                List<SendList> list = data.SendList.Where(p => p.PersonnelID == sPid && p.ReceiverDel == false && p.SendType == Convert.ToInt32(EnumData.SendType.短讯)).OrderByDescending(p => p.SendDate).Skip((currpageindex - 1) * pagerowcount).Take(pagerowcount).ToList();
                this.Rep_MySend.DataSource = data.SendList.Where(p => p.PersonnelID == sPid && p.ReceiverDel == false && p.SendType == Convert.ToInt32(EnumData.SendType.短讯)).OrderByDescending(p => p.SendDate).Skip((currpageindex - 1) * pagerowcount).Take(pagerowcount);
                this.Rep_MySend.DataBind();
                this.lab_pagecount.Text = pagecount.ToString();
            }
        }

        int i = 0;
        public int GetXid(string id)
        {
            i++;
            return (Convert.ToInt32(this.lab_CurrPage.Text) - 1) * Convert.ToInt32(this.lab_count.Text) + i;
        }
        #endregion

        #region 事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            this.BingRep();
        }

        protected void Rep_MySend_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            using (StoragePeriodDataContext data = new StoragePeriodDataContext())
            {
                int id = Convert.ToInt32(e.CommandArgument);
                if (e.CommandName == "del")
                {
                    SendList list = data.SendList.First(p => p.id == id);
                    list.ReceiverDel = true;
                    data.SubmitChanges();
                }
                else if (e.CommandName == "info")
                {
                    SendList list = data.SendList.First(p => p.id == id);
                    list.IsRead = true;
                    data.SubmitChanges();
                    this.txt_title.Text = list.Title;
                    this.txt_content.Text = list.SendContent;
                    ClientScript.RegisterStartupScript(this.GetType(), "show", "<script>$(document).ready(function () { divshow(); });</script>");
                }
            }
            this.BingRep();
        }
        
        protected void btn_First_Click(object sender, EventArgs e)
        {
            this.lab_CurrPage.Text = "1";
            this.BingRep();
        }

        protected void btn_Prev_Click(object sender, EventArgs e)
        {
            int currpage = Convert.ToInt32(this.lab_CurrPage.Text);
            if (currpage > 1)
                this.lab_CurrPage.Text = (currpage - 1).ToString();
            this.BingRep();
        }

        protected void btn_Next_Click(object sender, EventArgs e)
        {
            int currpage = Convert.ToInt32(this.lab_CurrPage.Text);
            if (currpage < Convert.ToInt32(this.lab_pagecount.Text))
                this.lab_CurrPage.Text = (currpage + 1).ToString();
            this.BingRep();
        }

        protected void btn_Last_Click(object sender, EventArgs e)
        {
            this.lab_CurrPage.Text = this.lab_pagecount.Text;
            this.BingRep();
        }
        #endregion
    }
}